package miner;

import java.sql.Connection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import lib.Db;
import lib.TableStatus;
import localreader.UserMa;
import localreader.UserMa2;
import tool.MStatus;
import weibo4j.model.Status;
import weibo4j.model.StatusWapper;

public class GetStatus {

	/**
	 * @param args
	 */
	public static void main(String[] args)  {
		UpdateStatusSet2(UserSort.advertising,"2405882664");
//		UpdateStatusSingle("1031262455");
//		deleteSet1Fol("1676400875");


	}

	public static void UpdateStatusSet2(UserSort sort, String begin){
		List<String> Users = getUser(sort);
		System.out.println("total users: "+Users.size());
		Iterator<String> index = Users.iterator();
		int skipNum = 0;
		
		if(begin != null){
			while(index.hasNext()){
				++skipNum;
				if(index.next().equals(begin)){
					break;
				}
			}
		}
		System.out.println("skip num: " + skipNum);
		
		Connection con = Db.connect();
		if(con == null){
			System.out.println("Connect to database failed");
			return;
		}
		System.out.println("get db handler ok.");
		while(index.hasNext()){
			String uid = index.next();
			StatusWapper receive = MStatus.UpdateStatusByUserId(uid);
			
			for(Status aStatus : receive.getStatuses()){
				if(TableStatus.existStatus(aStatus, con) != true){
					TableStatus.insertStatus(aStatus, con);
				}
			}

			System.out.println(uid+": get "+ receive.getStatuses().size()+" status.");
		}
		
		Db.disconnect();
		System.out.println("free db handler ok");
	}
	
	public static void UpdateStatusSingle(String uid){
		StatusWapper receive = MStatus.UpdateStatusByUserId(uid);
		
		Connection con = Db.connect();	
		if(con == null){
			System.out.println("Connect to database failed");
			return;
		}
		for(Status aStatus : receive.getStatuses()){
			if(TableStatus.existStatus(aStatus, con) != true){
				TableStatus.insertStatus(aStatus, con);
			}	
		}
		System.out.println(uid+": get "+ receive.getStatuses().size()+" status.");
		Db.disconnect();
	}
	
	private static List<String> getUser(UserSort sort){
		List<String> users = new LinkedList<String>();
		UserMa2 userMa2 = new UserMa2();
		
		switch(sort){
		case normal:
			users.addAll(userMa2.getNoUsers());
			break;
		case abnormal:
			users.addAll(userMa2.getAbUsers());
			break;
		case advertising:
			users.addAll(userMa2.getAdUsers());
			break;
		case dead:
			users.addAll(userMa2.getDeUsers());
			break;
		}
		
		return users;
	}
	
	public static void deleteSet1Fol(String begin){
		UserMa userMa = new UserMa();
		List<String> users = userMa.getAllFol();
		
		Iterator<String> index = users.iterator();
		
		if(begin != null){
			while(index.hasNext()){
				if(begin.equals(index.next())){
					break;
				}
			}
		}
		
		Connection con = Db.connect();
		if(con == null){
			System.out.println("Connect to database failed");
			return;
		}
		while(index.hasNext()){
			String uid = index.next();
			int count = TableStatus.deleteStatusByUid(uid, con);
			System.out.println(uid+": delete "+count+" status");
		}
		
		Db.disconnect();
		
	}
	
	enum UserSort {
		normal, abnormal, advertising, dead
	}

}

